package com.tripupdater.command
{
	import com.tripupdater.event.TUEvent;
	import com.tripupdater.event.TUEventDispatcher;
	import com.tripupdater.model.Model;
	import com.tripupdater.model.Params;
	import com.tripupdater.type.CommandType;
	import com.tripupdater.view.MapView;
	import com.tripupdater.view.SkypeView;
	import com.tripupdater.view.TripUpdaterMainView;
	import com.tripupdater.view.TwitterView;
	import com.turner.caf.commands.ICommand;
	import com.turner.caf.control.CAFEvent;
	
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.net.URLLoader;
	import flash.net.URLRequest;

	public class LoadAppDataCommand extends MovieClip implements ICommand
	{
		private var model:Model;
		private var evt:TUEventDispatcher;
		private var viewTarget:TripUpdaterMainView;
		private var mapView:MapView;
		private var skypeView:SkypeView;
		private var twitterView:TwitterView;
		private var altTwitterView:TwitterView;
		
		public function LoadAppDataCommand()
		{
			super();
			
			model= Model.getInstance();
			evt= TUEventDispatcher.getInstance();
		}

		public function execute(event:CAFEvent):void
		{
			viewTarget= event.viewTarget as TripUpdaterMainView;
			var visualElements:Object= event.data as Object;
			mapView= visualElements.mapView as MapView;
			skypeView= visualElements.skypeView as SkypeView;
			twitterView= visualElements.twitterView as TwitterView;
			altTwitterView= visualElements.altTwitterView as TwitterView;
			
			var loader:URLLoader= new URLLoader();
			loader.addEventListener(Event.COMPLETE, onComplete);
			loader.load(new URLRequest(Params.BASE_PATH + "xml/config.xml"));
		}
		
		private function onComplete(e:Event):void
		{
			var xml:XML= new XML(e.target.data);
			Params.ALT_TWITTER_OUTPUT= 	xml.altTwitterOutput.toString();
			Params.ALT_TWITTER_USER= 	xml.altTwitterUser.toString();
			Params.SKYPE_USER= 			xml.skypeUser.toString();
			Params.TWITTER_OUTPUT= 		xml.twitterOutput.toString();
			Params.TWITTER_USER= 		xml.twitterUser.toString();
			Params.GOOGLE_MAPS_KEY= 	xml.googleMapsKey.toString();
			
			(e.target as URLLoader).removeEventListener(Event.COMPLETE, onComplete);
			
			evt.dispatchEvent(new TUEvent(CommandType.LOAD_CALENDAR, null, this));
		}
		
		public function onCalendarLoaded():void
		{
			mapView.addEventListener(TUEvent.MAP_LOADED, onMapLoaded);
			mapView.createMap();
		}
		
		private function onMapLoaded(e:TUEvent):void
		{
			mapView.removeEventListener(TUEvent.MAP_LOADED, onMapLoaded);
			mapView.getPosition();
			
			skypeView.addEventListener(TUEvent.SKYPE_STATUS_LOADED, onSkypeStatusLoaded);
			skypeView.getSkypeStatus();
		}
		
		private function onSkypeStatusLoaded(e:TUEvent):void
		{
			skypeView.removeEventListener(TUEvent.SKYPE_STATUS_LOADED, onSkypeStatusLoaded);
			
			twitterView.addEventListener(TUEvent.TWITTER_TIMELINE_LOADED, onTwitterLoaded);
			twitterView.getTwitterTimeline(Params.TWITTER_USER);
		}
		
		private function onTwitterLoaded(e:TUEvent):void
		{
			twitterView.removeEventListener(TUEvent.TWITTER_TIMELINE_LOADED, onTwitterLoaded);
			
			altTwitterView.addEventListener(TUEvent.TWITTER_TIMELINE_LOADED, onAltTwitterLoaded);
			altTwitterView.getTwitterTimeline(Params.ALT_TWITTER_USER);
		}
		
		private function onAltTwitterLoaded(e:TUEvent):void
		{
			altTwitterView.removeEventListener(TUEvent.TWITTER_TIMELINE_LOADED, onAltTwitterLoaded);
			
			viewTarget.onReady();
		}
	}
}